
from random import sample
from .models import QuestionType,Question

class ProducePaper():
    '''
    功能：随机生成试卷题目
    dict_arg:是'paper/'提交的request.POST字典
    
    例子：{'单选题':['数量','分值']}
    
    self.info_dict:随机生成试题信息字典，
    例子：{'英汉互译题': [<Question: 7.    When do you get up everyday ?>,
    <Question: 4.teacher(     ) computer (       )>, <Question: try (     )>] }

    self.info_dict_pk()：将info_dict的值转变成pk值
    '''
    def __init__(self,paper_info):
        
        self.paper_info = paper_info
        self.paper_info_obj = {}
        #print(self.paper_info_obj)
        self.paper_info_pk = {}
        self.produce_paper_obj()

    def produce_paper_obj(self):
        
        for key,value in self.paper_info.copy().items():
        
            q_type = QuestionType.objects.get(question_type = key)
            #print(q_type)
            q_obj_list = list(q_type.question_set.all())
            
            q_obj_list_sample = sample(q_obj_list,value[0])
            
                    
            self.paper_info_obj[q_type.question_type] = value[:]
            self.paper_info_obj[q_type.question_type].append(q_obj_list_sample)

            q_pk_list_sample = [question.id for question in q_obj_list_sample]
           
            self.paper_info_pk[q_type.question_type] = value[:]
            self.paper_info_pk[q_type.question_type].append(q_pk_list_sample)


        
      
